package com.yzdbx.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.yzdbx.utils.DbUtil;
import com.yzdbx.utils.GenUtil;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.yzdbx.task.SalefOrderRunnable.GEN_ORDER_IDS;
import static com.yzdbx.utils.DbUtil.getMaxId;
import static com.yzdbx.utils.GenUtil.CUR_DATE;
import static com.yzdbx.utils.GenUtil.CUR_TIME_STAMP;

/**
 * @BelongsProject: SERVEVICE-DATA-GEN
 * @BelongsPackage: com.yzdbx.task
 * @Author: lk
 * @CreateTime: 2025-05-14
 * @Description: TODO
 * @Version: 1.0
 */
@SuppressWarnings("all")
public class ProductPlanRunnable implements Exe {


    private Connection connection;

    public static final ThreadLocal<List<Integer>> GEN_PRODUCT_PLAN_IDS = new ThreadLocal<>();

    public ProductPlanRunnable(Connection connection) {
        this.connection = connection;
    }

    public boolean execute(int count) throws SQLException {
        List<Integer> orderIds = GEN_ORDER_IDS.get();
        //根据销售订单生成生产计划
        if (CollectionUtil.isEmpty(orderIds))
            return Boolean.FALSE;

        int num = 0;

        List<Integer> planIds = new ArrayList<>();
        for (Integer orderId : orderIds) {
            //查询销售订单
            Map<String, Object> saleOrder = DbUtil.getById(connection, "salesOrders", orderId);
            if (saleOrder == null)
                continue;
            //查询销售订单明细
            PreparedStatement statement = connection.prepareStatement("select * from xs02SalesOrderBreakdown where associatedSalesOrders = ?");
            statement.setInt(1, orderId);
            ResultSet resultSet = statement.executeQuery();
            List<LinkedHashMap<String, Object>> details = DbUtil.resultSetToList(resultSet);

            int id = getMaxId(connection, "sc01ProductionPlan");

            for (LinkedHashMap<String, Object> detail : details) {
                PreparedStatement statement1 = connection.prepareStatement("insert into sc01ProductionPlan (id,scheduleNumber,clientName,dateOfDelivery,number,numberCompleted,productInformation,whetherOrNotArrangementsAreInPla,guanLianXiaoShouDingDan,synchronisingFolderFailedSS,created,modifyer,modified) values (?,?,?,?,?,?,?,?,?,?,?,?,?)");
                statement1.setInt(1, ++id);
                statement1.setString(2, GenUtil.getNo("JH", ++num));
                statement1.setInt(3, (int) saleOrder.get("clientInformation"));
                statement1.setDate(4, ((Date) saleOrder.get("dateOfOrder")));
                statement1.setBigDecimal(5, (BigDecimal) detail.get("number"));
                statement1.setBigDecimal(6, (BigDecimal) detail.get("number"));
                statement1.setInt(7, (int) detail.get("productInformation"));
                statement1.setString(8, "存储内容2");
                statement1.setInt(9, ((int) saleOrder.get("id")));
                statement1.setString(10, saleOrder.get("synchronisingFolderFailedSS").toString());
                statement1.setTimestamp(11, CUR_TIME_STAMP.get());
                statement1.setString(12, saleOrder.get("synchronisingFolderFailedSS").toString());
                statement1.setTimestamp(13, CUR_TIME_STAMP.get());
                statement1.execute();
                planIds.add(id);
            }


        }
        GEN_PRODUCT_PLAN_IDS.set(planIds);

        return Boolean.TRUE;
    }


}
